package com.mmall.concurrency.example.singleton;

import com.mmall.concurrency.annoation.NotRecommmend;
import com.mmall.concurrency.annoation.ThreadSafe;
import com.mmall.concurrency.example.sync.SynchronizedExample1;


/**
 * 懒汉模式--》双重同步锁单例模式
 * 单例实例在第一次使用时创建
 */
@ThreadSafe
@NotRecommmend
public class SingletonExample5 {

    //私有构造函数
    private SingletonExample5(){

    }

    //单例对象 volatile + 双重检测机制 -》禁止指令重排
    private volatile static SingletonExample5 instance = null;

    //静态的工厂方法
    public static SingletonExample5 getInstance(){

        if(instance == null){   //双重检测机制
            synchronized (SynchronizedExample1.class) { //同步锁
                if(instance == null) {
                    instance = new SingletonExample5();
                }
            }
        }
        return instance;
    }

}
